﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LogisticDatabase.Models;
using LogisticDatabase.Data;

namespace LogisticDatabase
{
    /// <summary>
    /// By Warhouse needed a complexer operation than basic three data operations
    /// </summary>
    public interface IWarhouseDataAccess : IDataAccess<Warehouse>
    {
        void updateProductCapacity(Warehouse warehouse);
    }

    public class WarehouseDataAccess : IWarhouseDataAccess
    {
        /// <summary>
        /// Modify a product capacity in a warehouse
        /// </summary>
        /// <param name="warehouse">Must contain: one element in product list (with id and capacity),
        /// warhouse id, </param>
        public void updateProductCapacity(Warehouse warehouse)
        {

            int productId = warehouse.products[0].Id;
            int capacity = warehouse.products[0].Capacity;

            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                var warehouseCapacity = ctx.tblWarehouseCapacity
                    .Include("tblWarehouse")
                    .Include("tblProduct")
                    .Where(t => t.tblWarehouse.WarehouseID == warehouse.Id 
                        && t.tblProduct.ProductID == productId)
                    .SingleOrDefault();

                if(warehouseCapacity!=null)
                {
                    warehouseCapacity.Quantity = capacity;
                    ctx.SaveChanges();
                }
            }
        }


        /// <summary>
        /// Gettin all warehouse with products data and factory
        /// </summary>
        /// <returns>list of warehouse object</returns>
        public List<Warehouse> listAll()
        {
            List<Warehouse> warehouses = new List<Warehouse>();
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                warehouses = ctx.tblWarehouse
                    .Include("tblFactory")
                    .Include("tblProduct")
                    .Include("tblWarehouseCapacity")
                    .Select(t => new Warehouse { Id = t.WarehouseID, Name = t.WarehouseName, Factory = new Factory 
                { Id = t.tblFactory.FactoryID, Name = t.tblFactory.FactoryName },
                                                 products = t.tblWarehouseCapacity.Select(tw => new Product
                                                 {
                                                      Id = tw.tblProduct.ProductID,
                                                      Name = tw.tblProduct.ProductName,
                                                      Capacity = tw.Quantity??0
                                                 }).ToList()
                    
                    
                    
                    }).ToList();

            }
            return warehouses;

        }

        /// <summary>
        /// Modifying warhouse name and owner factor id 
        /// </summary>
        /// <param name="warehouse">Warhouse object with warhouse id, new name and new owner factoryid
        /// (other member is optional)</param>
        public void edit(Warehouse warehouse)
        {
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                var c = ctx.tblWarehouse.FirstOrDefault(t => t.WarehouseID == warehouse.Id);
                if (c != null)
                {
                    c.WarehouseName = warehouse.Name;
                    c.FactoryNameFK = warehouse.Factory.Id;
                    //c.tblFactory = ctx.tblFactory.FirstOrDefault(t => t.FactoryID == warehouse.Factory.Id);
                    ctx.SaveChanges();
                }



            }
        }

        /// <summary>
        /// Adding new warehouse to database 
        /// </summary>
        /// <param name="warehouse"> Warhouse object, which contain new name, and new owner factory id</param>
        public void add(Warehouse warehouse)
        {
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                tblWarehouse tw = new tblWarehouse();

                tw.WarehouseName = warehouse.Name;
                //tw.tblFactory = ctx.tblFactory.FirstOrDefault(t => t.FactoryID == warehouse.Factory.Id);

                tw.FactoryNameFK = warehouse.Factory.Id;

                ctx.tblWarehouse.Add(tw);

                ctx.SaveChanges();
            }
        }

        /// <summary>
        /// Delete Warhouse entry from database
        /// </summary>
        /// <param name="warehouse">Requirements: Warhouse id</param>
        public void delete(Warehouse warehouse)
        {
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                var c = ctx.tblWarehouse.FirstOrDefault(t => t.WarehouseID == warehouse.Id);
                if (c != null)
                {
                    ctx.tblWarehouse.Remove(c);
                    ctx.SaveChanges();
                }
            }

        }
        

    }
}
